<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25 0025
 * Time: 10:32
 */

namespace app\admin\logic;

use app\common\traits\Gate;
use app\admin\model\AuthRule;
use app\admin\logic\Order as order_logic;
use nx\Data;
use think\Cache;
use think\Db;

class Common
{
    use Gate;

    /**
     * 根据角色获取渠道来源数据
     * @return false|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getQudao()
    {
        $depar = new Depar();
        $Qudao = new Qudao();
        $depar = collect($depar->getAllDepars())->where('status', 1);//所有的部门
        $Qudao = collect($Qudao->getAllSources())->where('status', 1);//所有的渠道来源
        $Source = $depar->where('id', session('admin_user.depar_id'))->first();//当前用户的部门 信息
        if ($this->hasRole(['qdzg']) || $this->hasRole('qdzy')) {//渠道主管 权限 和 渠道专员 权限
            if ($Source) {
                $where = explode(',', $Source['rule']);
                $data = $Qudao->whereIn('id', $where)->all();
            }
            return $data;
        } else {//其他权限的
            return $Qudao->all();
        }
    }

    /**
     * 根据渠道获取来源
     * @param $data
     * @return false|\PDOStatement|string|\think\Collection
     * @author:wu
     */
    public function getChennelInSource(array $data = [])
    {
        if (empty($data)) {
            return Db::name('origin')->where('status', 1)->select();
        }
        $laiyuaid = Db::name('qudao')
            ->whereIn('id', $data)
            ->column('rule');

        $laiyuaid = join(',', $laiyuaid);
        $laiyuaid = array_unique(explode(',', $laiyuaid));
        return Db::name('origin')->whereIn('id', $laiyuaid)->select();
    }

    /**
     * 根据来源获取班级
     * @param $data
     * @author:wu
     */
    public function getSourceInBanji(array $data = [])
    {
        $user = session('admin_user');

        $aa = '';
        if ($this->hasRole([ 'qdzy', 'qdkf'])) {
//            dump($aa);die();
            if ($this->hasRole('line', $user['adminid'])) {
                $aa = true;
            } else {
                $aa = false;
            }
        } elseif ($this->hasRole([ 'yyzg', 'yyzy'])) {
            if ($this->hasRole('line', $user['adminid'])) {
                $aa = true;
            } else {
                $aa = false;
            }

        }
//        dump($aa);die();
        if($aa==''){

        }
        if ($aa === false){
            return json(array('role'=>0 ,'msg'=>'您没有权限访问'));
        }
        if (empty($data)) {
            return Db::name('banji')->select();
        }
//        dump($data);
        $laiyuaid = Db::name('qudao')
            ->whereIn('id', $data)
            ->column('banji_rule');
        $laiyuaid = join(',', $laiyuaid);
        $laiyuaid = array_unique(explode(',', $laiyuaid));
        return Db::name('banji')->whereIn('id', $laiyuaid)->select();
    }

    /**
     * 获取所有的用户
     * @return array|mixed
     * @author: Huang
     */
    public function getAllUserInfo()
    {
        $getUserInfo = new User();
        $userinfo = Cache::tag('alluser')->get('userinfo');
        if (empty($userinfo)) {
            $userinfo = collect($getUserInfo->getAllUsers());
            Cache::tag('alluser')->set('userinfo', $userinfo);
        }
        return $userinfo;
    }

    /**
     * 根据id来获取用户信息
     * @param $uid
     * @param bool $force
     * @return mixed
     * @author: Huang
     */
    public function getOneUserById($uid, $force = false)
    {
        $userinfo = Cache::tag('alluser')->get('userinfo' . $uid);
        if (empty($userinfo) || $force) {
            $userinfo = collect($this->getAllUserInfo())->where('id', $uid)->first();
            Cache::tag('alluser')->set('userinfo' . $uid, $userinfo);
        }
        return $userinfo;
    }

    /**
     * 根据id来获取用户信息
     * @param $uid
     * @return mixed
     * @author: Huang
     */
    public function getOneUserBysId($uid)
    {
        $getUserInfo = new User();
        $userinfo = collect($getUserInfo->getAllUsers())->where('id', $uid)->first();
        return $userinfo;
    }

    /**
     * 根据pid获取当前的子权限
     * @param $pid
     * @return Classify|array|mixed
     * @author: Huang
     */
    public function getAuthByPid($pid)
    {
        $auth = Auth::instance();
        $uid = session('admin_user.adminid');
        $auth_ids_list = $auth->getAuthList($uid, 1, 'id');
        $temp = collect(Cache::get('Auth_' . $pid));
        $temps = $temp->pluck('id')->all();
        $res = array_intersect($auth_ids_list, $temps);
        $AutMenu = $temp->whereIn('id', $res)->all();
        if (!empty($AutMenu)) return $AutMenu;

        $AutMenu = new AuthRule();
        if (empty($AutMenu)) return $AutMenu;
        $AutMenu = collect($AutMenu->getAllAuths())->where('status', 0)->where('pid', $pid)->all();
        Cache::set('Auth_' . $pid, $AutMenu);
        return $AutMenu;
    }

    /**
     * 获取所有部门
     * @return array|mixed
     * @author: Huang
     */
    public function getAllDepar()
    {
        $getdepar = new Depar();
        $deparinfo = Cache::tag('alldepar')->get('deparinfo');
        if (empty($deparinfo)) {
            $deparinfo = collect($getdepar->getAllDepars());
            Cache::tag('alldepar')->set('deparinfo', $deparinfo);
        }
        return $deparinfo;
    }

    /**
     * 获取子部门id
     * @param $deparid
     * @return array
     * @author: Huang
     */
    public function getSonDepar($deparid)
    {
        $alldepar = Db::name('depar')->where('status', 1)->cache(600)->select();
        $depar = Data::channelList($alldepar, $deparid);
        return $depar;
    }

    function getSondeparId($id)
    {
        $alldepar = \think\Db::name('depar')->where('status', 1)->cache(600)->select();
        $depar = \nx\Data::channelList($alldepar, $id);
        $depar = collect($depar)->column('id');
        $depar[] = (int)$id;
        return $depar;
    }

    /**
     * 根据部门和角色获取部门下用户信息/门市亦同/邀约亦同
     * @param $depar_id
     * @return array|mixed
     * @author: Guo
     */
    public function getUserByDeparId($depar_id, $auth = null)
    {
        $auth = explode(',', $auth);
        if (!empty($auth[0])) {
            $userinfo = collect(Cache::tag('alluser')->get('user' . $depar_id))->whereIn('auth_id', $auth)->all();
        } else {
            $userinfo = collect(Cache::tag('alluser')->get('user' . $depar_id))->all();
        }
        if (!current($userinfo)) {
            $getUserInfo = new User();
            $alluserinfo = collect($getUserInfo->getAllUsers());
            $depar = collect($this->getSonDepar($depar_id))->pluck('id')->all();
            $depar = array_merge([$depar_id], $depar);
//            $userids = Db::name('userdepar')->where('depar_id', 'in', $depar)->column('user_id');
            if (!empty($auth[0])) {
                $userinfo = $alluserinfo->whereIn('depar_id', $depar)->whereIn('auth_id', $auth)->all();
            } else {
                $userinfo = $alluserinfo->whereIn('depar_id', $depar)->all();
            }
            Cache::tag('alluser')->set('user' . $depar_id, $userinfo);
        }
        return $userinfo;
    }

    /**
     * 根据部门获取部门下用户信息/门市亦同/邀约亦同 发送微信信息
     * @param $depar_id
     * @return array|mixed
     * @author: Guo
     */
    public function getUserByDeparWx($depar_id)
    {
        $userinfo = Cache::tag('alluser')->get('wxuser' . $depar_id);
        if (empty($userinfo)) {
            $getUserInfo = new User();
            $alluserinfo = collect($getUserInfo->getAllUsers());
            $depar = collect($this->getSonDepar($depar_id))->pluck('id')->all();
            $depar = array_merge([$depar_id], $depar);
            $userinfo = $alluserinfo->whereIn('depar_id', $depar)->all();
            Cache::tag('alluser')->set('wxuser' . $depar_id, $userinfo);
        }
        return $userinfo;
    }

    /**
     * 根据部门id得到部门名称
     * @param $depar_id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getDeparNameById($depar_id)
    {
        $deparname = Cache::tag('depar')->get('deparname' . $depar_id);
        if (empty($deparname)) {
            $deparname = Depar::deparsToText()[$depar_id];
            Cache::tag('depar')->set('deparname' . $depar_id, $deparname);
        }
        return $deparname;
    }


    /**
     * 根据订单id获取订单的操作日志
     * @param $orderid
     * @return array|false|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getOrderLogById($orderid)
    {
        $orderlog = new OrderLog();
        $orderdata = $orderlog->getModel()->where('order_id', $orderid)->order('create_time desc')->select();
        if (empty($orderdata)) return $orderdata;
        $orderdata = collection($orderdata)->append([
            'czaction_text', 'khd_text', 'czry_text', 'state_text', 'depar_id_text'
        ])->toArray();
        return $orderdata;
    }

    /**
     * 根据pid获取当前的数据
     * @param $pid
     * @return Classify|array|mixed
     * @author: Huang
     */
    protected function getCurrency($pid)
    {
        $Intention = Cache::get('Intention_' . $pid);
        if (!empty($Intention)) return $Intention;
        $Intention = new Classify();
        if (empty($Intention)) return $Intention;
        $Intention = collect($Intention->getAllClassifys())->where('status', 1)->where('pid', $pid)->all();
        Cache::set('Intention_' . $pid, $Intention);
        return $Intention;
    }

    /**
     * 获取意向
     * @return Classify|array|mixed
     * @author: Huang
     */
    public function getIntention()
    {
        return $this->getCurrency(1);
    }

    /**
     * 获取预算
     * @return Classify|array|mixed
     * @author: Huang
     */
    public function getBudget()
    {
        return $this->getCurrency(12);
    }

    /**
     * 获取类别
     * @return Classify|array|mixed
     * @author: Huang
     */
    public function getLeibie()
    {
        $data = $this->getCurrency(3);
        $data = array_merge($data);
        return $data;
    }

    /**
     * 获取所有的活动
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getAllActivity()
    {
        $activity = new Activity();
        return $activity->getAllActivitys();
    }

    /**
     * 获取所有的区域
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getAllArea()
    {
        $area = new Area();
        return $area->getAllAreas();
    }

    /**
     * 获取支付方式
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getPayment()
    {
        return $this->getCurrency(20);
    }

    /**
     * 获取门店
     * @return false|\PDOStatement|string|\think\Collection
     * @author: Huang
     */
    public function getStore()
    {
        $store = Cache::tag('store')->get('mendian');
        if (empty($store)) {
            $store = Db::name('depar')->where('status', 1)->where('pid', 2)->select();
            Cache::tag('store')->set('mendian', $store);
        }
        return $store;
    }

    /**
     * 获取返无效原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getInvalid()
    {
        return $this->getCurrency(32);
    }

    /**
     * 获取进店流失原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getInstore()
    {
        return $this->getCurrency(30);
    }

    /**
     * 获取无需追踪原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getTracking()
    {
        return $this->getCurrency(29);
    }

    /**
     * 获取下次追踪原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getTrack()
    {
        return $this->getCurrency(33);
    }


    /**
     * 获取未到店流失原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function noStore()
    {
        return $this->getCurrency(31);
    }


    /**
     * 获取流失原因
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getLsyy()
    {
        $wxzz = $this->getCurrency(29);//无需追踪流失原因
        $jdls = $this->getCurrency(30);//进店流失
        $wdd = $this->getCurrency(31);//未到店
        $fwx = $this->getCurrency(32);//返无效
        $xczz = $this->getCurrency(33);//下次追踪
        return array_merge($wxzz, $jdls, $wdd, $fwx, $xczz);
    }

    /**
     * 获取拍摄时间范围
     * @return Classify|array|mixed
     * @author: Huang
     */
    public function getStimate()
    {
        return $this->getCurrency(39);
    }

    /**
     * 获取意向拍摄次数
     * @return Classify|array|mixed
     * @author: Guo
     */
    public function getInt_num()
    {
        return $this->getCurrency(42);
    }

    /**
     * 获取拍摄方式
     * @return Classify|array|mixed
     * @author: wu
     */
    public function getShotMode()
    {
        return $this->getCurrency(47);
    }

    /**
     * 导入订单类型转换
     * @return array
     * @author: Huang
     */
    public function TypesToint()
    {
        $arr = collect($this->getLeibie())->column('id', 'title');
        return $arr;
    }

    /**
     * 导入拍摄方式转换
     * @return array
     * @author: Huang
     */
    public function ShotModeToint()
    {
        $arr = collect($this->getShotMode())->column('id', 'title');
        return $arr;
    }

    /**
     * 导入性别转换
     * @return array
     * @author: Huang
     */
    public function SexToint()
    {
        $arr = ['女' => 0, '男' => 1];
        return $arr;
    }

    /**
     * 订单状态的数量
     * @param $data
     * @return int
     */
    public function getState($data)
    {
        // 校验权限
        $order_logic = new order_logic;
        $data1 = $order_logic->power();
        $orderData = Db::name('order')->where($data1)->where('status', 1)->cache(600)->select();
        $count = collect($orderData)->where('state', $data)->count();
        return $count;
    }
}